﻿using Magicodes.ExporterAndImporter.Excel.AspNetCore;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NP.BPMReportPlatform.Entities.BPM_Business;
using NP.BPMReportPlatform.Holiday.Dto;
using NP.BPMReportPlatform.HR.Dto;
using NP.BPMReportPlatform.MaterialMasterData.Dto;
using NP.BPMReportPlatform.PR;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;

namespace NP.BPMReportPlatform.MaterialMasterData
{
    /// <summary>
    /// 物料主数据流程 接口
    /// </summary>
    [Authorize]
    [ApiController]
    [Route("api/mmd")]
    public class MMDController : BPMReportPlatformController
    {
        private readonly IMMDOrderService _mMDOrderService;


        public MMDController(IMMDOrderService mMDOrderService)
        {
            _mMDOrderService = mMDOrderService;
        }

        /// <summary>
        ///立才中心认证需求管理流程报表 查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("LCZXRZXQGL/GetAllAsync")]
        public async Task<PagedResultDto<GetLCZXRZXQGLOutput>> GetAllAsync([FromBody] LCZXRZXQGLInput input)
        {
            return await _mMDOrderService.GetAllAsync(input);
        }

        /// <summary>
        /// 立才中心认证需求管理流程报表 导出数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("LCZXRZXQGL/ExportAsync")]
        public async Task<XlsxFileResult<GetLCZXRZXQGLOutput>> ExportAsync([FromBody] LCZXRZXQGLInput input)
        {
            var rst = await _mMDOrderService.ExportAsync(input);
            var result = new List<GetLCZXRZXQGLOutput>();
            return new XlsxFileResult<GetLCZXRZXQGLOutput>(ObjectMapper.Map(rst, result), "导出Excel");
        }

        /// <summary>
        ///国家证书 查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("LCZXRZXQGL_NC/GetAllAsync")]
        public async Task<PagedResultDto<GetLCZXRZXQGLNCOutput>> GetAllAsync([FromBody] LCZXRZXQGLNCInput input)
        {
            return await _mMDOrderService.GetAllAsync(input);
        }

        /// <summary>
        /// 国家证书批量导入Excel
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("LCZXRZXQGL_NC/ImporterExcelAsync")]
        public async Task<ImportResultExt<ImportLCZXInput>> ImporterExcelAsync(IFormFile file)
        {
            if (file == null)
            {
                var res = new ImportResultExt<ImportLCZXInput>();
                res.Exception = new Exception("请上传文件！");
                return res;
            }
            var s = file.OpenReadStream();
            var bytes = StreamToBytes(s);
            //var file = Request.Form.Files[0];
            return await _mMDOrderService.ImporterLCZXExcel(bytes);
        }
        private byte[] StreamToBytes(Stream stream)
        {
            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);
            // 设置当前流的位置为流的开始
            stream.Seek(0, SeekOrigin.Begin);
            return bytes;
        }

        /// <summary>
        /// 国家证书 导出
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("LCZXRZXQGL_NC/ExportAsync")]
        public async Task<XlsxFileResult<GetLCZXRZXQGLNCOutput>> ExportAsync([FromBody] LCZXRZXQGLNCInput input)
        {
            var rst = await _mMDOrderService.ExportAsync(input);
            var result = new List<GetLCZXRZXQGLNCOutput>();
            return new XlsxFileResult<GetLCZXRZXQGLNCOutput>(ObjectMapper.Map(rst, result), "导出Excel");
        }

        /// <summary>
        /// 编辑保存国家证书
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("LCZXRZXQGL_NC/EditNationalData")]
        public async Task<string> EditNationalData([FromBody] LCZXRZXQGLDetail input)
        {
            return await _mMDOrderService.EditNationalData(input);
        }

        /// <summary>
        /// 导出国家认证信息导入模板
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("LCZXRZXQGL_NC/ExportTemplate")]
        public async Task<XlsxFileResult<LCZXNCImportTemplate>> ExportTemplateAsync()
        {
            return new XlsxFileResult<LCZXNCImportTemplate>(new List<LCZXNCImportTemplate>(), "国家认证信息导入模板");
        }

        [HttpDelete]
        [Route("LCZXRZXQGL_NC/DeleteNationalData")]
        public async Task<bool> DeleteNationalData([FromQuery] Guid Id)
        {
            return await _mMDOrderService.DeleteNationalData(Id);
        }
        #region 预算数据导入
        /// <summary>
        ///排课平台预算数据导入 查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("PKBudgetData/GetPKBudgetDataAllAsync")]
        public async Task<PagedResultDto<GetPKBudgetDataOutput>> GetPKBudgetDataAllAsync([FromBody] PKBudgetDataInput input)
        {
            return await _mMDOrderService.GetPKBudgetDataAllAsync(input);
        }

        /// <summary>
        /// 排课平台预算数据导入批量导入Excel
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("PKBudgetData/ImporterPKBudgetDataExcelAsync")]
        public async Task<ImportResultExt<ImportPKBudgetDataInput>> ImporterPKBudgetDataExcelAsync(IFormFile file)
        {
            if (file == null)
            {
                var res = new ImportResultExt<ImportPKBudgetDataInput>();
                res.Exception = new Exception("请上传文件！");
                return res;
            }
            var s = file.OpenReadStream();
            var bytes = StreamToBytes(s);
            //var file = Request.Form.Files[0];
            return await _mMDOrderService.ImporterPKBudgetDataExcel(bytes);
        }

        /// <summary>
        ///排课平台预算数据导入 导出
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("PKBudgetData/ExportPKBudgetDataAsync")]
        public async Task<XlsxFileResult<PKBudgetDataOutput>> ExportPKBudgetDataAsync([FromBody] PKBudgetDataInput input)
        {
            var rst = await _mMDOrderService.ExportPKBudgetDataAsync(input);
            var result = new List<PKBudgetDataOutput>();
            return new XlsxFileResult<PKBudgetDataOutput>(ObjectMapper.Map(rst, result), "导出Excel");
        }
        /// <summary>
        ///排课平台预算数据导入 模板导出
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("PKBudgetData/ExportPKBudgetModelAsync")]
        public async Task<XlsxFileResult<PKBudgetDataOutput>> ExportPKBudgetModelAsync()
        {
            var result = new List<PKBudgetDataOutput>();
            return new XlsxFileResult<PKBudgetDataOutput>(ObjectMapper.Map("", result), "导出Excel");
        }

        /// <summary>
        /// 获取排课平台 培训认证中心/项目组
        /// </summary>
        /// <returns></returns>
        [Route("PKBudgetData/GetSchoolNameList")]
        [HttpGet]
        public async Task<PagedResultDto<GetPKBudgetDataOutput>> GetSchoolNameList()
        {
            return await _mMDOrderService.GetSchoolNameList();
        }
        /// <summary>
        /// 排课平台预算数据导入编辑
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("PKBudgetData/EditPKBudgetDataAsync")]
        public async Task<string> EditPKBudgetDataAsync([FromBody] ImportPKBudgetDataInput input)
        {
            var res = "成功";
            if (input.Id != Guid.Empty)
            {
                PKBudgetDataInput dataInput = new PKBudgetDataInput()
                {
                    Year = input.Year,
                    Month = input.Month,
                    SchoolName = input.SchoolName,
                    id = input.Id.ToString(),
                    PageIndex = 1,
                    PageSize = 1
                };
                var data = await GetPKBudgetDataAllAsync(dataInput);
                if (data != null && data.TotalCount >= 1)
                {
                    if (data.Items[0].Id != input.Id)
                    {
                        res = "存在重复的'年份-月份-培训认证中心/项目组预算数据维护'！";
                    }
                    else
                    {
                        await _mMDOrderService.EditAsync(input);
                    }
                }
                else
                {
                    await _mMDOrderService.EditAsync(input);
                }
            }
            else
            {
                res = "失败";
            }
            return res;
        }

        /// <summary>
        /// 排课平台预算数据导入删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        [Route("PKBudgetData/DeletePKBudgetDataAsync")]
        public async Task DeletePKBudgetDataAsync([FromQuery] Guid id)
        {
            await _mMDOrderService.DeleteAsync(id);
        }
        #endregion

        #region 校长提交
        /// <summary>
        ///排课平台校长提交 主表查询历史
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("PKSubmittedByPrincipal/GetPKSubmittedByPrincipalHistory")]
        public async Task<PagedResultDto<GetPKSubmittedByPrincipalOutput>> GetPKSubmittedByPrincipalHistory([FromBody] PKBudgetDataInput input)
        {
            return await _mMDOrderService.GetPKSubmittedByPrincipalHistory(input);
        }

        /// <summary>
        /// 获取开始链接
        /// </summary>
        /// <returns></returns>
        [Route("PKSubmittedByPrincipal/GetStartUrl")]
        [HttpGet]
        public async Task<string> GetStartUrl()
        {
            return await _mMDOrderService.GetStartUrl();
        }
        #endregion
        #region 校长提交实际
        /// <summary>
        ///排课平台校长实际提交 主表查询历史
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("PKSubmittedByPrincipal/GetPKSubmittedByPrincipalRealHistory")]
        public async Task<PagedResultDto<GetPKSubmittedByPrincipalRealOutput>> GetPKSubmittedByPrincipalRealHistory([FromBody] PKBudgetDataInput input)
        {
            return await _mMDOrderService.GetPKSubmittedByPrincipalRealHistory(input);
        }

        /// <summary>
        /// 获取校长提交实际开始链接
        /// </summary>
        /// <returns></returns>
        [Route("PKSubmittedByPrincipal/GetRealStartUrl")]
        [HttpGet]
        public async Task<string> GetRealStartUrl()
        {
            return await _mMDOrderService.GetRealStartUrl();
        }
        #endregion

        #region 财务报表
        /// <summary>
        ///排课平台校长提交 明细查询分页
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("PKSubmittedByPrincipal/GetPKSubmittedByPrincipalDetailSearch")]
        public async Task<PagedResultDto<GetPKSubmittedByPrincipalDetailFinancialOutput>> GetPKSubmittedByPrincipalDetailSearch([FromBody] PKBudgetDataInput input)
        {
            //return await _mMDOrderService.GetPKSubmittedByPrincipalDetailSearch(input);
            return await _mMDOrderService.GetPKSubmittedByPrincipalDetailFinancialSearch(input);
        }

        /// <summary>
        /// 财务报表 明细导出数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("PKSubmittedByPrincipal/ExportDetailAsync")]
        public async Task<XlsxFileResult<GetPKSubmittedByPrincipalDetailFinancialOutput>> ExportDetailAsync([FromBody] PKBudgetDataInput input)
        {
            var rst = await _mMDOrderService.GetPKSubmittedByPrincipalDetailFinancial(input);
            var result = new List<GetPKSubmittedByPrincipalDetailFinancialOutput>();
            return new XlsxFileResult<GetPKSubmittedByPrincipalDetailFinancialOutput>(ObjectMapper.Map(rst, result), "导出Excel");
        }
        #endregion
        #region 排班报表
        /// <summary>
        ///排课平台校长提交 排班报表明细查询分页
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("PKSubmittedByPrincipal/GetPKSubmittedByPrincipalDetailSchdueSearch")]
        public async Task<PagedResultDto<GetPKSubmittedByPrincipalDetailSchdueOutput>> GetPKSubmittedByPrincipalDetailSchdueSearch([FromBody] PKBudgetDataInput input)
        {
            //return await _mMDOrderService.GetPKSubmittedByPrincipalDetailSearch(input);
            return await _mMDOrderService.GetPKSubmittedByPrincipalDetailSchdueSearch(input);
        }

        /// <summary>
        /// 排班报表 明细导出数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("PKSubmittedByPrincipal/ExportDetailSchdueAsync")]
        public async Task<XlsxFileResult<GetPKSubmittedByPrincipalDetailSchdueOutput>> ExportDetailSchdueAsync([FromBody] PKBudgetDataInput input)
        {
            var rst = await _mMDOrderService.GetPKSubmittedByPrincipalDetailSchdue(input);
            var result = new List<GetPKSubmittedByPrincipalDetailSchdueOutput>();
            return new XlsxFileResult<GetPKSubmittedByPrincipalDetailSchdueOutput>(ObjectMapper.Map(rst, result), "导出Excel");
        }
        #endregion
        #region 全景图规划月

        /// <summary>
        /// 全景图规划月取值
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("PKMonthPanorama/GetAllAsync")]
        public async Task<List<GetPKMonthPanoramaOutPut>> GetAllAsync([FromBody] PKMonthPanoramaInput input)
        {
            return await _mMDOrderService.GetMonthPanorama(input);
        }
        #endregion
        #region 全景图实际月

        /// <summary>
        /// 全景图实际月取值
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("PKMonthPanorama/GetAllAsyncReal")]
        public async Task<List<GetPKMonthPanoramaOutPut>> GetAllAsyncReal([FromBody] PKMonthPanoramaInput input)
        {
            return await _mMDOrderService.GetMonthReal(input);
        }
        #endregion

        #region 全景图对比
        /// <summary>
        /// 全景图对比取值
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("PKMonthPanorama/GetAllAsyncRealandEstimate")]
        public async Task<List<GetPKMonthPanoramaOutPut>> GetAllAsyncRealandEstimate([FromBody] PKMonthPanoramaInput input)
        {
            return await _mMDOrderService.GetMonthRealandEstimate(input);
        }
        #endregion
        #region 假期维护
        /// <summary>
        /// 节假日查询
        /// </summary>
        /// <param name="input">参数</param>
        /// <returns>查询结果</returns>
        [HttpPost]
        [Route("PKHoliday/GetHolidays")]
        public async Task<List<ExportHoliday>> GetHolidays(HolidaySearchDto input)
        {
            return await _mMDOrderService.GetHolidays(input);
        }

        /// <summary>
        /// 节假日详情查询
        /// </summary>
        /// <param name="input">参数</param>
        /// <returns>查询结果</returns>
        [HttpPost]
        [Route("PKHoliday/GetHolidaysDetail")]
        public async Task<ExportHoliday> GetHolidaysDetail(HolidaySearchDto input)
        {
            if (input.holidayDate != null)
            {
                string holidayDate = DateTime.Parse(input.holidayDate.ToString()).ToString("yyyy-MM-dd");
                return await _mMDOrderService.GetHolidaysDetail(new HolidaySearchDto() { holidayDate = holidayDate });
            }
            return null;
        }

        /// <summary>
        /// 节假日导出
        /// </summary>
        /// <param name="input">查询条件</param>
        [HttpPost]
        [Route("PKHoliday/ExportHoliday")]
        public async Task<XlsxFileResult<ExportHoliday>> ExportHoliday(HolidaySearchDto input)
        {
            var rst = await _mMDOrderService.ExportHoliday(input);
            var result = new List<ExportHoliday>();
            return new XlsxFileResult<ExportHoliday>(ObjectMapper.Map(rst, result), "导出Excel");
        }

        /// <summary>
        /// 添加节假日
        /// </summary>
        /// <param name="input">添加数据</param>
        [HttpPost]
        [Route("PKHoliday/AddHoliday")]
        public async Task<string> AddHoliday([FromBody] HolidayInputDto input)
        {
            var res = "成功";
            if (!string.IsNullOrEmpty(input.HolidayDate) && !string.IsNullOrEmpty(input.Status.ToString()))
            {
                DateTime HolidayDate = DateTime.Parse(input.HolidayDate);
                int year = HolidayDate.Year;
                int month = HolidayDate.Month;
                int day = HolidayDate.Day;
                HolidayInput dataInput = new HolidayInput()
                {
                    HolidayDate = HolidayDate.ToString("yyyy-MM-dd"),
                    HolidayYear = year,
                    HolidayMonth = month,
                    HolidayDay = day,
                    Status = input.Status,
                    Remark = input.Remark,
                };
                await _mMDOrderService.AddHoliday(dataInput);
            }
            else
            {
                res = "失败";
            }
            return res;
        }


        /// <summary>
        ///  导入节假日
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("PKHoliday/ImportProcess")]
        public async Task<ImportResultExt<ExportHoliday>> ImportProcess(IFormFile file)
        {
            if (file == null)
            {
                var res = new ImportResultExt<ExportHoliday>();
                res.Exception = new Exception("请上传文件！");
                return res;
            }
            var s = file.OpenReadStream();
            var bytes = StreamToBytes(s);
            return await _mMDOrderService.ImportProcess(bytes);
        }

        /// <summary>
        ///导入节假日 模板导出
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("PKHoliday/ExportHolidayModelAsync")]
        public async Task<XlsxFileResult<ExportHoliday>> ExportHolidayModelAsync()
        {

            return new XlsxFileResult<ExportHoliday>(new List<ExportHoliday>(), "节假日维护模板导出");
        }

        /// <summary>
        /// 初始化本年度周六末，传入年份
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("PKHoliday/SaveAllHoliday")]
        public async Task<string> SaveAllHoliday([FromBody] HolidayInputDto input)
        {
            var res = "成功";
            if (input.Year == 0)
            {
                res = "失败";
            }
            else {
                await _mMDOrderService.SaveAllHoliday(input.Year.ToString());
            }
            return res;
        }
        #endregion
    }
}